//
// Created by fairy on 2025/7/28.
//

#ifndef LT_HPP
#define LT_HPP


#include "LT768/LT768_Lib.hpp"

namespace bsp::lcd
{
    class LT768 {
        using lcd_spi = hal::spi2;

    public:
        [[gnu::always_inline]] static void init()
        {
            LT768_Init();
        }

        static void flush(const uint16_t x1, const uint16_t y1, const uint16_t x2, const uint16_t y2, uint8_t *data)
        {
            // 使用DMA的情况
            while (HAL_DMA_GetState(lcd_spi::getDMAHandle_tx()) != HAL_DMA_STATE_READY) {
            }

            Graphic_Mode();
            Active_Window_XY(x1, y1);
            Active_Window_WH(x2 - x1 + 1, y2 - y1 + 1);
            Goto_Pixel_XY(x1, y1);
            LCD_CmdWrite(0x04);

            lcd_spi::cs_low();
            hal::spi2::send_byte(0x80); //模拟RS拉低
            const uint32_t pixel_count = (x2 - x1 + 1) * (y2 - y1 + 1); // 计算像素数量

            HAL_DMA_Start_IT(lcd_spi::getDMAHandle_tx(),
                             reinterpret_cast<uint32_t>(data), // 直接使用uint16_t*地址
                             reinterpret_cast<uint32_t>(&(lcd_spi::getHandle()->Instance->DR)), // SPI数据寄存器地址
                             pixel_count << 1); // 像素数量*2
            // SPI2向DMA发出请求
            lcd_spi::getDMAHandle_rx()->Instance->CR |= DMA_IT_TC;
        }
    };
}

#endif //LT_HPP
